using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class NewMainMenu_HypotheticalSubMenu : MonoBehaviour
{
	public List<TextMeshProUGUI> BackgroundTextLines = new List<TextMeshProUGUI>();

	public TextMeshProUGUI MainText;

	[SerializeField]
	private List<int> BackgroundTextMaxCount = new List<int>();

	private int CurrentSelected;

	private int previousSelected = -1;

	[SerializeField]
	private List<Hypothesis> PlayableHypotheticals = new List<Hypothesis>();

	private bool wasEnabled;

	[SerializeField]
	private GameObject ConsoleMenu;

	[SerializeField]
	private NewMainMenu_Hypothetical_ContextMenu HypotheticalContextMenu;

	public Sprite noIconSprite;

	[SerializeField]
	private List<string> storedHypotheticalNames = new List<string>();

	[SerializeField]
	private TextMeshProUGUI HypotheticalName;

	[SerializeField]
	private TextMeshProUGUI HypotheticalTagline;

	[SerializeField]
	private Image HypotheticalIcon;

	private bool AllTextShown;

	public bool AllowInput;

	private void Awake()
	{
		PlayableHypotheticals.Insert(0, null);
		CachePrefabReturnedValues();
		foreach (TextMeshProUGUI backgroundTextLine in BackgroundTextLines)
		{
			BackgroundTextMaxCount.Add(((TMP_Text)backgroundTextLine).text.Length);
			((TMP_Text)backgroundTextLine).maxVisibleCharacters = 0;
		}
		UpdateUnlockedGamblingState();
	}

	private void Update()
	{
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		//IL_0086: Unknown result type (might be due to invalid IL or missing references)
		//IL_009d: Unknown result type (might be due to invalid IL or missing references)
		if (!GonerMenu.Instance.GonerMenuOpen && AllowInput)
		{
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Down))
			{
				CurrentSelected++;
				CheckCurrentSelectedOutsideBounds();
				CheckUpdateScreenText();
			}
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Up))
			{
				CurrentSelected--;
				CheckCurrentSelectedOutsideBounds();
				CheckUpdateScreenText();
			}
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Confirm) || Input.GetKeyDown(PlayerInput.Instance.Key_Menu))
			{
				SelectHypothetical();
			}
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Cancel))
			{
				NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuDeny);
				ConsoleMenu.SetActive(true);
				((Component)this).gameObject.SetActive(false);
			}
		}
		if (!wasEnabled && ((Component)this).gameObject.activeSelf)
		{
			wasEnabled = true;
			AllowInput = true;
			CurrentSelected = 1;
			previousSelected = -1;
			((MonoBehaviour)this).StartCoroutine(ShowBackgroundText());
			CheckUpdateScreenText();
			UpdateUnlockedGamblingState();
		}
	}

	private void UpdateUnlockedGamblingState()
	{
		if (PlayerPrefs.GetInt("UnlockedGambling", 0) != 0)
		{
			return;
		}
		bool flag = true;
		foreach (Hypothesis playableHypothetical in PlayableHypotheticals)
		{
			if ((Object)(object)playableHypothetical != (Object)null && playableHypothetical.HypotheticalReleased)
			{
				if (PlayerPrefs.GetInt("TimesPlayed_" + playableHypothetical.HypothesisName, 0) <= 0)
				{
					flag = false;
					break;
				}
				HypothesisGoal[] hypothesisGoals = playableHypothetical.HypothesisGoals;
				foreach (HypothesisGoal hypothesisGoal in hypothesisGoals)
				{
					if (PlayerPrefs.GetInt("HypothesisGoal_" + hypothesisGoal.GoalPlayprefName, 0) < hypothesisGoal.RequiredTasks)
					{
						flag = false;
						break;
					}
				}
			}
			if (!flag)
			{
				break;
			}
		}
		if (flag)
		{
			PlayerPrefs.SetInt("UnlockedGambling", 1);
			GonerMenu.Instance.ShowTip("主菜单似乎解锁了某个选项...");
		}
	}

	private void OnDisable()
	{
		wasEnabled = false;
		previousSelected = -1;
		AllTextShown = false;
		ChangeMainText("", AllowAnimation: false);
		foreach (TextMeshProUGUI backgroundTextLine in BackgroundTextLines)
		{
			((TMP_Text)backgroundTextLine).maxVisibleCharacters = 0;
		}
	}

	private void CachePrefabReturnedValues()
	{
		storedHypotheticalNames.Clear();
		foreach (Hypothesis playableHypothetical in PlayableHypotheticals)
		{
			if ((Object)(object)playableHypothetical != (Object)null)
			{
				storedHypotheticalNames.Add(playableHypothetical.HypothesisName);
			}
			else
			{
				storedHypotheticalNames.Add("... Return");
			}
		}
	}

	private void SelectHypothetical()
	{
		if (!((Object)(object)PlayableHypotheticals[CurrentSelected] != (Object)null) || PlayableHypotheticals[CurrentSelected].HypotheticalReleased)
		{
			AllowInput = false;
			previousSelected = -1;
			NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuSelect);
			((MonoBehaviour)this).StartCoroutine(SelectHypotheticalTimer());
			CheckUpdateScreenText();
		}
		else if (PlayableHypotheticals[CurrentSelected].HypotheticalReleased)
		{
			AllowInput = false;
			previousSelected = -1;
			NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuSelect);
			((MonoBehaviour)this).StartCoroutine(SelectHypotheticalTimer());
			CheckUpdateScreenText();
		}
		else
		{
			NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuDeny);
		}
	}

	private IEnumerator SelectHypotheticalTimer()
	{
		yield return (object)new WaitForSeconds(0.5f);
		if ((Object)(object)PlayableHypotheticals[CurrentSelected] != (Object)null)
		{
			HypotheticalContextMenu.CurrentHypothesis = PlayableHypotheticals[CurrentSelected];
			((Component)HypotheticalContextMenu).gameObject.SetActive(true);
		}
		else
		{
			NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuDeny);
			ConsoleMenu.SetActive(true);
			((Component)this).gameObject.SetActive(false);
		}
	}

	private void CheckCurrentSelectedOutsideBounds()
	{
		if (CurrentSelected < 0)
		{
			CurrentSelected = PlayableHypotheticals.Count - 1;
		}
		if (CurrentSelected > PlayableHypotheticals.Count - 1)
		{
			CurrentSelected = 0;
		}
		NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuMove);
	}

	private void CheckUpdateScreenText()
	{
		//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_03c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_0248: Unknown result type (might be due to invalid IL or missing references)
		if (previousSelected == CurrentSelected)
		{
			return;
		}
		previousSelected = CurrentSelected;
		string text = "";
		for (int i = 0; i < storedHypotheticalNames.Count; i++)
		{
			if ((Object)(object)PlayableHypotheticals[i] == (Object)null)
			{
				if (CurrentSelected == i)
				{
					text += (AllowInput ? "<color=yellow>-> ... 返回</color>\n" : "<color=green>-> ... 返回</color>\n");
					((TMP_Text)HypotheticalName).text = "";
					((TMP_Text)HypotheticalTagline).text = "";
					HypotheticalIcon.sprite = noIconSprite;
					((Graphic)HypotheticalIcon).color = new Color(1f, 1f, 1f, 0f);
				}
				else
				{
					text += "... 返回\n";
				}
			}
			else
			{
				if (!PlayableHypotheticals[i].HypotheticalReleased)
				{
					continue;
				}
				bool flag = IsAllQuestsCompleted(i);
				string text2 = (flag ? "yellow" : "white");
				if (PlayableHypotheticals[i].HypothesisGoals.Length == 0 && PlayerPrefs.GetInt("TimesPlayed_" + PlayableHypotheticals[i].HypothesisName, 0) > 0)
				{
					text2 = "yellow";
				}
				if (CurrentSelected == i)
				{
					string text3 = (flag ? "#ffc478" : "#fffc78");
					if (PlayableHypotheticals[i].HypothesisGoals.Length == 0 && PlayerPrefs.GetInt("TimesPlayed_" + PlayableHypotheticals[i].HypothesisName, 0) > 0)
					{
						text3 = "#ffc478";
					}
					text += (AllowInput ? ("<color=" + text3 + ">-> " + storedHypotheticalNames[i] + "</color>\n") : ("<color=green>-> " + storedHypotheticalNames[i] + "</color>\n"));
					((TMP_Text)HypotheticalName).text = storedHypotheticalNames[i];
					((TMP_Text)HypotheticalTagline).text = PlayableHypotheticals[i].HypothesisTagline;
					((Graphic)HypotheticalIcon).color = new Color(1f, 1f, 1f, 1f);
					HypotheticalIcon.sprite = PlayableHypotheticals[i].HypothesisMenuSprite ?? noIconSprite;
				}
				else
				{
					text = text + "<color=" + text2 + ">" + storedHypotheticalNames[i] + "</color>\n";
				}
			}
		}
		text += "\n<size=42><color=red>> 以下猜想正在制作 <</size><color=grey>\n\n";
		for (int j = 0; j < storedHypotheticalNames.Count; j++)
		{
			if ((Object)(object)PlayableHypotheticals[j] != (Object)null && !PlayableHypotheticals[j].HypotheticalReleased)
			{
				if (CurrentSelected == j)
				{
					text += (AllowInput ? ("<color=yellow>-> " + storedHypotheticalNames[j] + "</color>\n") : ("<color=green>-> " + storedHypotheticalNames[j] + "</color>\n"));
					((TMP_Text)HypotheticalName).text = storedHypotheticalNames[j];
					((TMP_Text)HypotheticalTagline).text = PlayableHypotheticals[j].HypothesisTagline;
					HypotheticalIcon.sprite = noIconSprite;
					((Graphic)HypotheticalIcon).color = new Color(1f, 1f, 1f, 1f);
				}
				else
				{
					text = text + "<color=grey>" + storedHypotheticalNames[j] + "</color>\n";
				}
			}
		}
		text += "\n<color=green>敬请期待！</color>";
		ChangeMainText(text, AllowAnimation: true);
	}

	private IEnumerator ShowBackgroundText()
	{
		if (!SettingsManager.Instance.GetBoolSettingValue("SimpleVFX"))
		{
			List<TextMeshProUGUI> finishedTexts = new List<TextMeshProUGUI>();
			while (!AllTextShown)
			{
				yield return null;
				foreach (TextMeshProUGUI backgroundTextLine in BackgroundTextLines)
				{
					if (!finishedTexts.Contains(backgroundTextLine) && ((TMP_Text)backgroundTextLine).maxVisibleCharacters >= ((TMP_Text)backgroundTextLine).text.Length)
					{
						finishedTexts.Add(backgroundTextLine);
					}
					else if (((TMP_Text)backgroundTextLine).maxVisibleCharacters < ((TMP_Text)backgroundTextLine).text.Length)
					{
						float num = 100 + Random.Range(-60, -10);
						((TMP_Text)backgroundTextLine).maxVisibleCharacters = ((TMP_Text)backgroundTextLine).maxVisibleCharacters + Mathf.CeilToInt(num * Time.deltaTime);
						((TMP_Text)backgroundTextLine).maxVisibleCharacters = Mathf.Clamp(((TMP_Text)backgroundTextLine).maxVisibleCharacters, 0, ((TMP_Text)backgroundTextLine).text.Length);
						NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuClick);
					}
				}
				if (CompareTextLists(finishedTexts, BackgroundTextLines))
				{
					AllTextShown = true;
					MonoBehaviour.print((object)"all text shown");
				}
			}
			yield break;
		}
		foreach (TextMeshProUGUI backgroundTextLine2 in BackgroundTextLines)
		{
			((TMP_Text)backgroundTextLine2).maxVisibleCharacters = ((TMP_Text)backgroundTextLine2).text.Length;
		}
	}

	private bool CompareTextLists(List<TextMeshProUGUI> list1, List<TextMeshProUGUI> list2)
	{
		if (list1.Count != list2.Count)
		{
			return false;
		}
		for (int i = 0; i < list1.Count; i++)
		{
			if (((TMP_Text)list1[i]).text != ((TMP_Text)list2[i]).text)
			{
				return false;
			}
		}
		return true;
	}

	public void ChangeMainText(string text, bool AllowAnimation)
	{
		((TMP_Text)MainText).text = text;
		((TMP_Text)MainText).maxVisibleCharacters = text.Length;
	}

	private IEnumerator ScrollMainText()
	{
		yield return null;
		while (((TMP_Text)MainText).maxVisibleCharacters < ((TMP_Text)MainText).text.Length)
		{
			float num = 600 + Random.Range(-120, 0);
			TextMeshProUGUI mainText = MainText;
			((TMP_Text)mainText).maxVisibleCharacters = ((TMP_Text)mainText).maxVisibleCharacters + Mathf.CeilToInt(num * Time.deltaTime);
			((TMP_Text)MainText).maxVisibleCharacters = Mathf.Clamp(((TMP_Text)MainText).maxVisibleCharacters, 0, ((TMP_Text)MainText).text.Length);
			NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuClick);
			yield return null;
		}
	}

	[CompilerGenerated]
	private bool IsAllQuestsCompleted(int hypotheticalIndex)
	{
		HypothesisGoal[] hypothesisGoals = PlayableHypotheticals[hypotheticalIndex].HypothesisGoals;
		if (hypothesisGoals == null || hypothesisGoals.Length == 0)
		{
			return false;
		}
		HypothesisGoal[] array = hypothesisGoals;
		foreach (HypothesisGoal hypothesisGoal in array)
		{
			if (PlayerPrefs.GetInt("HypothesisGoal_" + hypothesisGoal.GoalPlayprefName, 0) < hypothesisGoal.RequiredTasks)
			{
				return false;
			}
		}
		return true;
	}
}
